/**
 * Definition for a binary tree node.
 * function TreeNode(val) {
 *     this.val = val;
 *     this.left = this.right = null;
 * }
 */
/**
 * @param {TreeNode} A
 * @param {TreeNode} B
 * @return {boolean}
 */

var isSubStructure = function (A, B) {
    if (!B || !A) {
        return false
    }
    return searchNode(A, B)
    // 寻找第一个val相等的结点
    function searchNode(root1, root2) {
        if (!root1) {
            return false
        }
        if (root1.val === root2.val && judge(root1, root2)) {
            return true
        }
        return searchNode(root1.left, root2) || searchNode(root1.right, root2)
    }
    // 判断是否结构相同
    function judge(root1, root2) {
        if (!root2) {
            return true
        }
        if (!root1) {
            return false
        }
        return root1.val === root2.val && judge(root1.left, root2.left) && judge(root1.right, root2.right)
    }
};